package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICarPackageAuditService  carPackageAuditService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else if (ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldServiceItem = this.get(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(oldServiceItem.getSaleStatus())) {
            throw new BusinessException("商品上架状态不可修改");
        } else if (ServiceItem.AUDITSTATUS_AUDITING.equals(oldServiceItem.getAuditStatus())) {
            throw new BusinessException("商品审核中状态不可修改");
        }
        if (!(ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(oldServiceItem.getAuditStatus()))) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItem.setVersion(oldServiceItem.getVersion());
        serviceItemMapper.updateByPrimaryKey(serviceItem);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void updateSaleOn(Long id) {
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())
                && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            throw new BusinessException("套餐服务需要审核通过才能上架");
        }
        serviceItemMapper.updateSaleOnStatus(id, ServiceItem.SALESTATUS_ON);
    }


    @Override
    public void updateSaleOff(Long id) {
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.updateSaleOffStatus(id, ServiceItem.SALESTATUS_OFF, ServiceItem.AUDITSTATUS_INIT);
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void changeAuditStatus(Long id, Integer auditStatusInit) {
        serviceItemMapper.changeAuditStatus(id,auditStatusInit);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        ServiceItem serviceItem = this.get(id);
        if (serviceItem == null || ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())
                || !ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus())
                || ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())) {
            throw new BusinessException("非法参数,不符合审核要求");
        }
        serviceItemMapper.changeAuditStatus(id, ServiceItem.AUDITSTATUS_AUDITING);
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(id);
        audit.setServiceItemInfo(serviceItem.getInfo());
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        audit.setInfo(info);
        audit.setCreateTime(new Date());
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setAuditorId(director);
        audit.setCreator(ShiroUtils.getUser().getUserName());

        carPackageAuditService.save(audit);
        Map<String, Object> map = new HashMap<>();
        if (director != null) {
            map.put("director", director);
        }
        if (finance != null) {
            map.put("finance", finance);
        }
        String businessKey = audit.getId().toString();
        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        map.put("discountPrice", serviceItem.getDiscountPrice().longValue());
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), businessKey, map);
        carPackageAuditService.updateByInstanceId(audit.getId(),processInstance.getProcessInstanceId());
    }
}
